package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.jrcrm.dao.cms.CmsUserStateLogDao;
import net.dgg.dggcloud.service.jrcrm.entity.cms.CmsUserStateLog;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.service.user.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 鲜林材
 * 用户状态记录service
 * @create 2017-11-21 14:07
 **/
@Service
public class CmsUserStateLogService extends BaseService{

    @Resource
    private CmsUserStateLogDao cmsUserStateLogDao;

    @Resource
    private UserService userService;

    /**
     * 保存记录
     * @param cmsUserStateLog:记录实体
     * @param user:用户
     */
    @Transactional
    public void save(CmsUserStateLog cmsUserStateLog,User user){
        try {
            cmsUserStateLog.setCreateUser(user);
            cmsUserStateLog.setId(this.keyWorker.nextId());
            this.repository.save(cmsUserStateLog);
        }catch (Exception e){
            e.printStackTrace();
            throw  new BizException("用户状态改变记录异常:"+e.getMessage(),10005);
        }
    }

    /**
     * 改变用户状态
     * @param cmsUserStateLog
     * @param currentLoginUser
     */
    @Transactional
    public void saveUserState(CmsUserStateLog cmsUserStateLog, User currentLoginUser) {
        try{
            User user = userService.getUserById(currentLoginUser.getId());
            user.setUpdaterUser(currentLoginUser);
            user.setStateType(cmsUserStateLog.getStateType());
            this.repository.saveOrUpdate(user);
            this.save(cmsUserStateLog,currentLoginUser);
        }catch (Exception e){
            e.printStackTrace();
            throw  new BizException("改变用户状态异常:"+e.getMessage(),10006);
        }
    }


    public Integer countUserStateLogByMap(Map map){
        return  cmsUserStateLogDao.countUserStateLogByMap(map);
    }

    public List<Map> listUserStateLogByMap(Map map) {
        return cmsUserStateLogDao.listUserStateLogByMap(map);
    }

}
